package com.mrx.xfriendship.telnet.command.impl;

import com.mrx.xfriendship.telnet.command.AbstractCommand;
import com.mrx.xfriendship.telnet.command.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Mr.X
 * @since 2023-01-23 13:43
 */
@Command("help")
public class HelpCommand extends AbstractCommand {

    private static final Logger logger = LoggerFactory.getLogger(HelpCommand.class);

    @Resource
    private ApplicationContext app;

    private List<AbstractCommand> commandList;

    @Override
    protected Object doProcess(String command) {
        if (commandList == null) {
            commandList = Arrays.stream(app.getBeanNamesForAnnotation(Command.class))
                    .map(it -> (AbstractCommand) app.getBean(it))
                    .collect(Collectors.toList());
        }
        logger.debug("commandList: {}", commandList);
        return commandList.stream()
                .map(it -> it.getClass().getAnnotation(Command.class).value())
                .filter(it -> it.length != 0)
                .map(it -> it[0])
                .collect(Collectors.toList());
    }

}
